package 其他.按照字符数量从小到大输出;

import java.util.*;

/**
 一、给出一个 字符串，你需要统计每个字母出现的次数，并按照次数从小到大分别输出字母和次数
 示例 1:
 输入: aababcabcd
 输出:
 d,c,b,a
 1,2,3,4

 */
public class Main {
    public static HashMap<Character, Integer> map;

    public static void main(String[] args) {


        String s ="aababcabcd";

        map=new HashMap<>();


        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)){
                Integer integer = map.get(c);
                map.put(c,++integer);
            }else {
                map.put(c,1);
            }
        }


        Set<Character> set = map.keySet();
        ArrayList<Integer> list = new ArrayList<>();
        for (Character character : set) {
            list.add(map.get(character));
        }
        Collections.sort(list);
        ArrayList<Character> arrayList = new ArrayList<>();
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (Character character : set) {
            hashMap.put(character,map.get(character));
        }
        for (int i = 0; i < list.size(); i++) {
            Set<Character> set1 = map.keySet();
            for (Character character : set1) {
                if (map.get(character)==list.get(i)){
                    arrayList.add(character);
                    map.remove(character);
                    break;
                }
            }
        }


        for (Character character : arrayList) {
            System.out.print(character+",");
        }
        System.out.println();
        for (Character character : arrayList) {
            System.out.print(hashMap.get(character)+",");
        }


    }
}
